/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.edu.unju.fi.reservahabitaciones.grupo14.struts.modelo.dao.impl;

import ar.edu.unju.fi.reservahabitaciones.grupo14.struts.hibernate.HibernateUtil;
import ar.edu.unju.fi.reservahabitaciones.grupo14.struts.modelo.dao.CategoriaDao;
import ar.edu.unju.fi.reservahabitaciones.grupo14.struts.modelo.dominio.Categoria;
import java.math.BigDecimal;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author mauriljb
 */
public class CategoriaDaoImpl implements CategoriaDao{

    @Override
    public List<Categoria> getAll() {
         Session session = ar.edu.unju.fi.reservahabitaciones.grupo14.struts.hibernate.HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Categoria.class);
            criteria.addOrder(Order.asc("categoriaId"));
            List<Categoria> lista = (List<Categoria>) criteria.list();
            session.close();
            return lista;
    }

    @Override
    public void insert(Categoria cat) {
       Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(cat);
            session.getTransaction().commit();
            session.close();
    }

    @Override
    public void delete(Categoria cat) {
        Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(cat);
            session.getTransaction().commit();
            session.close();
    }

    @Override
    public void update(Categoria cat) {
       Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(cat);
            session.getTransaction().commit();
            session.close();
    }

    @Override
    public Categoria getCategoria(BigDecimal id) {
       Session session = HibernateUtil.getSessionFactory().openSession();
       session.beginTransaction();
       Criteria criteria = session.createCriteria(Categoria.class);
       criteria.add(Restrictions.eq("id", id));
       Categoria categoria = (Categoria) criteria.list().get(0);
       session.close();
       return categoria;
    }
     @Override
     public Categoria getCategoriaById(BigDecimal categoriaId){
      Session session = HibernateUtil.getSessionFactory().openSession();      
      Criteria criteria = session.createCriteria(Categoria.class);
      criteria.add(Restrictions.eq("categoriaId", categoriaId));
     
      return (Categoria) criteria.uniqueResult();
    }
    
}
